#include "NetworkPacket.h"
#include <stdlib.h>
#include <string.h>
#include <Log.h>
#include <sstream>
#include <iomanip>
#include <boost/crc.hpp>

using namespace CSNetworkLib;

bool NetworkPacket::IsResponse() const
{
	return m_response;
}

void NetworkPacket::SetResponse(bool val) const
{
	m_response = val;
}

bool NetworkPacket::IsWaitingForResponse() const
{
	return m_waiting;
}

void NetworkPacket::SetWaitingForResponse(bool val) const
{
	m_waiting = val;
}

uint16_t NetworkPacket::GetId() const
{
	return m_id;
}

void NetworkPacket::SetId(uint16_t id)
{
	m_id = id;
}

uint32_t NetworkPacket::GetSeq() const
{
	return m_seq;
}

void NetworkPacket::SetSeq(uint32_t seq) const
{
	m_seq = seq;
}

bool NetworkPacket::Push_Bool(bool value)
{
	struct FPacketData it;
	it.type = DATA_BOOL;
	it.size = sizeof(bool);
	it.data.mBool = value;
	m_items.push_back(it);
	return true;
}

bool NetworkPacket::Push_S8(int8_t value)
{
	struct FPacketData it;
	it.type = DATA_S8;
	it.size = sizeof(int8_t);
	it.data.mS8 = value;
	m_items.push_back(it);
	return true;
}

bool NetworkPacket::Push_U8(uint8_t value)
{
	struct FPacketData it;
	it.type = DATA_U8;
	it.size = sizeof(uint8_t);
	it.data.mU8 = value;
	m_items.push_back(it);
	return true;
}

bool NetworkPacket::Push_S16(int16_t value)
{
	struct FPacketData it;
	it.type = DATA_S16;
	it.size = sizeof(int16_t);
	it.data.mS16 = value;
	m_items.push_back(it);
	return true;
}

bool NetworkPacket::Push_U16(uint16_t value)
{
	struct FPacketData it;
	it.type = DATA_U16;
	it.size = sizeof(uint16_t);
	it.data.mU16 = value;
	m_items.push_back(it);
	return true;
}

bool NetworkPacket::Push_S32(int32_t value)
{
	struct FPacketData it;
	it.type = DATA_S32;
	it.size = sizeof(int32_t);
	it.data.mS32 = value;
	m_items.push_back(it);
	return true;
}

bool NetworkPacket::Push_U32(uint32_t value)
{
	struct FPacketData it;
	it.type = DATA_U32;
	it.size = sizeof(uint32_t);
	it.data.mU32 = value;
	m_items.push_back(it);
	return true;
}

bool NetworkPacket::Push_S64(int64_t value)
{
	struct FPacketData it;
	it.type = DATA_S64;
	it.size = sizeof(int64_t);
	it.data.mS64 = value;
	m_items.push_back(it);
	return true;
}

bool NetworkPacket::Push_U64(uint64_t value)
{
	struct FPacketData it;
	it.type = DATA_U64;
	it.size = sizeof(uint64_t);
	it.data.mU64 = value;
	m_items.push_back(it);
	return true;
}

bool NetworkPacket::Push_Float(float value)
{
	struct FPacketData it;
	it.type = DATA_FLOAT;
	it.size = sizeof(float);
	it.data.mFloat = value;
	m_items.push_back(it);
	return true;
}

bool NetworkPacket::Push_Double(double value)
{
	struct FPacketData it;
	it.type = DATA_DOUBLE;
	it.size = sizeof(double);
	it.data.mDouble = value;
	m_items.push_back(it);
	return true;
}

bool NetworkPacket::Push_String(const char * value)
{
	if (NULL == value) return false;
	struct FPacketData it;
	it.type = DATA_STRING;
	it.size = 0;
	while(value[it.size]!=0)
	{
		it.size++;
	}
	it.size++;
	it.data.mString = new char[it.size];
	memcpy(it.data.mString, value, it.size);
	m_items.push_back(it);
	return true;
}

bool NetworkPacket::Push_WideString(const uint16_t * value)
{
	if (NULL == value) return false;
	struct FPacketData it;
	it.type = DATA_WIDE_STRING;
	it.size = 0;
	while(value[it.size]!=0)
	{
		it.size++;
	}
	it.size++;
	it.data.mWideString = new uint16_t[it.size];
	memcpy(it.data.mWideString, value, it.size*2);
	it.size *= sizeof(uint16_t);
	m_items.push_back(it);
	return true;
}

bool NetworkPacket::Push_Data(const uint8_t* value, uint32_t size)
{
	if ((NULL == value) || (0 == size)) return false;
	struct FPacketData it;
	it.type = DATA_RAW;
	it.size = size;
	it.data.mData = new uint8_t[size];
	memcpy(it.data.mData, value, size);
	m_items.push_back(it);
	return true;
}

uint32_t NetworkPacket::GetCount() const
{
	return m_items.size();
}

uint32_t NetworkPacket::GetSize() const
{
	uint32_t size = 8+sizeof(uint32_t); /* header */
	for(uint32_t i = 0; i<m_items.size(); ++i)
	{
		size += m_items[i].size+1;
		if (DATA_RAW == m_items[i].type)
			size += 4;
	}
	return size;
}

 enum NetworkPacket::EDataType NetworkPacket::GetItemType(uint32_t index) const
 {
	return (index<m_items.size()) ? m_items[index].type : DATA_NONE;
 }

 uint32_t NetworkPacket::GetItemSize(uint32_t index) const
 {
	return (index<m_items.size()) ? m_items[index].size : 0;
 }

bool NetworkPacket::Get_Bool(uint32_t index, bool &value) const
{
	if (index>=m_items.size()) return false;
	if (m_items[index].type != DATA_BOOL) return false;
	value = m_items[index].data.mBool;
	return true;
}

bool NetworkPacket::Get_S8(uint32_t index, int8_t &value) const
{
	if (index>=m_items.size()) return false;
	if (m_items[index].type != DATA_S8) return false;
	value = m_items[index].data.mS8;
	return true;
}

bool NetworkPacket::Get_U8(uint32_t index, uint8_t &value) const
{
	if (index>=m_items.size()) return false;
	if (m_items[index].type != DATA_U8) return false;
	value = m_items[index].data.mU8;
	return true;
}

bool NetworkPacket::Get_S16(uint32_t index, int16_t &value) const
{
	if (index>=m_items.size()) return false;
	if (m_items[index].type != DATA_S16) return false;
	value = m_items[index].data.mS16;
	return true;
}

bool NetworkPacket::Get_U16(uint32_t index, uint16_t &value) const
{
	if (index>=m_items.size()) return false;
	if (m_items[index].type != DATA_U16) return false;
	value = m_items[index].data.mU16;
	return true;
}

bool NetworkPacket::Get_S32(uint32_t index, int32_t &value) const
{
	if (index>=m_items.size()) return false;
	if (m_items[index].type != DATA_S32) return false;
	value = m_items[index].data.mS32;
	return true;
}

bool NetworkPacket::Get_U32(uint32_t index, uint32_t &value) const
{
	if (index>=m_items.size()) return false;
	if (m_items[index].type != DATA_U32) return false;
	value = m_items[index].data.mU32;
	return true;
}

bool NetworkPacket::Get_S64(uint32_t index, int64_t &value) const
{
	if (index>=m_items.size()) return false;
	if (m_items[index].type != DATA_S64) return false;
	value = m_items[index].data.mS64;
	return true;
}

bool NetworkPacket::Get_U64(uint32_t index, uint64_t &value) const
{
	if (index>=m_items.size()) return false;
	if (m_items[index].type != DATA_U64) return false;
	value = m_items[index].data.mU64;
	return true;
}

bool NetworkPacket::Get_Float(uint32_t index, float &value) const
{
	if (index>=m_items.size()) return false;
	if (m_items[index].type != DATA_FLOAT) return false;
	value = m_items[index].data.mFloat;
	return true;
}

bool NetworkPacket::Get_Double(uint32_t index, double &value) const
{
	if (index>=m_items.size()) return false;
	if (m_items[index].type != DATA_DOUBLE) return false;
	value = m_items[index].data.mDouble;
	return true;
}

bool NetworkPacket::Get_String(uint32_t index, char * value, uint32_t max_size) const
{
	if (index>=m_items.size()) return false;
	if (m_items[index].type != DATA_STRING) return false;
	if (m_items[index].size>max_size) return false;
	memcpy(value, m_items[index].data.mString, m_items[index].size);
	return true;
}

std::string NetworkPacket::GetString(uint32_t index, const std::string &def) const
{
	if (index>=m_items.size()) return def;
	if (m_items[index].type != DATA_STRING) return def;
	if (0 == m_items[index].size) return def;
	return std::string(m_items[index].data.mString, m_items[index].size-1);
}

bool NetworkPacket::Get_WideString(uint32_t index, uint16_t * value, uint32_t max_size) const
{
	if (index>=m_items.size()) return false;
	if (m_items[index].type != DATA_WIDE_STRING) return false;
	if (m_items[index].size>max_size) return false;
	memcpy(value, m_items[index].data.mWideString, m_items[index].size);
	return true;
}

bool NetworkPacket::Get_Data(uint32_t index, uint8_t* value, uint32_t max_size) const
{
	if (index>=m_items.size()) return false;
	if (m_items[index].type != DATA_RAW) return false;
	if (m_items[index].size>max_size) return false;
	memcpy(value, m_items[index].data.mData, m_items[index].size);
	return true;
}

const uint8_t* NetworkPacket::Get_DataPtr(uint32_t index) const
{
	if (index>=m_items.size()) return NULL;
	if (m_items[index].type != DATA_RAW) return NULL;
	return m_items[index].data.mData;
}

static const uint16_t crc_table[256] = {
  0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
  0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
  0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
  0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
  0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
  0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
  0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
  0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
  0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
  0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
  0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
  0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
  0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
  0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
  0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
  0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
  0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
  0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
  0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
  0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
  0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
  0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
  0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
  0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
  0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
  0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
  0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
  0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
  0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
  0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
  0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
  0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
};

/* CRC calculation macros */
#define CRC_INIT 0xFFFF
#define CRC(crcval,newchar) crcval = (crcval >> 8) ^ \
	crc_table[(crcval ^ newchar) & 0x00ff]

uint16_t NetworkPacket::CalculateCRC16(const uint8_t* data, uint32_t size)
{
  uint16_t crc = CRC_INIT;
  for(uint32_t i = 0; i < size; i++)
  {
      CRC(crc, data[i]);
  }
  return crc;
}

std::string ToHex( const uint8_t* buffer, size_t size )
{
	std::stringstream str;
	str.setf(std::ios_base::hex, std::ios::basefield);
	str.setf(std::ios_base::uppercase);
	str.fill( '0' );

	for ( size_t i=0; i<size; ++i )
	{
		str << std::hex << std::setw(2) << (uint16_t)(uint8_t)buffer[i];
	}
	return str.str();
}

bool NetworkPacket::Serialize(uint8_t *data, uint32_t size) const
{
	FUNCTION_TRACK
	if (NULL == data) return false;
	uint32_t sSize = GetSize();
	if (size<sSize) return false;
	uint8_t *ptr = &data[8+sizeof(uint32_t)];
	DBUG("==== BEGIN PACKET SERIALIZATION =====");
	DBUG("Size = %u", sSize);
	DBUG("ID = %u", m_id);
	DBUG("Seq = %u", m_seq & 0x0FFFFFFF);
	DBUG("Response = %c", m_response?'Y':'N');
	DBUG("Waiting = %c", m_waiting?'Y':'N');
	for(uint32_t i = 0; i<m_items.size(); ++i)
	{
		*ptr = (uint8_t)m_items[i].type;
		ptr++;
		switch(m_items[i].type)
		{
			case DATA_BOOL :   *ptr = m_items[i].data.mBool?1:0;++ptr;DBUG("%d - BOOL : %d", i+1, m_items[i].data.mBool?1:0); break;
			case DATA_S8   :   *((uint8_t*)ptr) = m_items[i].data.mU8;ptr+=m_items[i].size;DBUG("%d - S8 : %d", i+1, m_items[i].data.mS8);break;
			case DATA_U8   :   *((uint8_t*)ptr) = m_items[i].data.mU8;ptr+=m_items[i].size;DBUG("%d - U8 : %u", i+1, m_items[i].data.mU8);break;
			case DATA_S16  :   *((uint16_t*)ptr) = switchEndian<uint16_t>(m_items[i].data.mU16);ptr+=m_items[i].size;DBUG("%d - S16 : %d", i+1, m_items[i].data.mS16);break;
			case DATA_U16  :   *((uint16_t*)ptr) = switchEndian<uint16_t>(m_items[i].data.mU16);ptr+=m_items[i].size;DBUG("%d - U16 : %u", i+1, m_items[i].data.mU16);break;
			case DATA_S32  :   *((uint32_t*)ptr) = switchEndian<uint32_t>(m_items[i].data.mU32);ptr+=m_items[i].size;DBUG("%d - S32 : %ld", i+1, m_items[i].data.mS32);break;
			case DATA_U32  :   *((uint32_t*)ptr) = switchEndian<uint32_t>(m_items[i].data.mU32);ptr+=m_items[i].size;DBUG("%d - U32 : %lu", i+1, m_items[i].data.mU32);break;
			case DATA_S64  :   *((uint64_t*)ptr) = switchEndian<uint64_t>(m_items[i].data.mU64);ptr+=m_items[i].size;DBUG("%d - S64 : %lld", i+1, m_items[i].data.mS64);break;
			case DATA_U64  :   *((uint64_t*)ptr) = switchEndian<uint64_t>(m_items[i].data.mU64);ptr+=m_items[i].size;DBUG("%d - U64 : %llu", i+1, m_items[i].data.mU64);break;
			case DATA_FLOAT :  *((float*)ptr) = m_items[i].data.mFloat;ptr+=m_items[i].size;DBUG("%d - Float : %f", i+1, m_items[i].data.mFloat);break;
			case DATA_DOUBLE : *((double*)ptr) = m_items[i].data.mDouble;ptr+=m_items[i].size;DBUG("%d - Double : %lf", i+1, m_items[i].data.mDouble);break;
			case DATA_WIDE_STRING : memcpy(ptr, m_items[i].data.mData,m_items[i].size); ptr+=m_items[i].size; DBUG("%d - WideString : 0x%s", i+1, ToHex(m_items[i].data.mData, m_items[i].size).c_str());break;
			case DATA_STRING : memcpy(ptr, m_items[i].data.mData,m_items[i].size); ptr+=m_items[i].size; DBUG("%d - String : 0x%s : '%s'", i+1, ToHex(m_items[i].data.mData, m_items[i].size).c_str(), m_items[i].data.mData); break;
			case DATA_RAW : *((uint32_t*)ptr) = switchEndian<uint32_t>(m_items[i].size);ptr+=4;
							memcpy(ptr, m_items[i].data.mData,m_items[i].size); ptr+=m_items[i].size;
							DBUG("%d - Raw : 0x%s", i+1, ToHex(m_items[i].data.mData, m_items[i].size).c_str());break;
		};
	}
	*((uint32_t*)data) = switchEndian<uint32_t>(sSize);
	*((uint16_t*)&data[4]) = switchEndian<uint16_t>(m_id);
	*((uint16_t*)&data[6]) = 0;
	uint32_t t2 = m_seq & 0x0FFFFFFF;
	if (m_response) t2 |= 0x10000000;
	if (m_waiting) t2 |= 0x20000000;
	*((uint32_t*)&data[8]) = switchEndian<uint32_t>(t2);

	if (sSize+sizeof(uint32_t)>8)
	{
		uint16_t crc16 = CalculateCRC16(&data[8+sizeof(uint32_t)], sSize-8-sizeof(uint32_t));
		*((uint16_t*)&data[6]) = switchEndian<uint16_t>(crc16);
		DBUG("Crc16 = 0x%X", crc16);
	}
	DBUG("HEX = %s", ToHex(data, sSize).c_str());
	DBUG("==== END PACKET SERIALIZATION =====");
	return true;
}

bool NetworkPacket::UnSerialize(const uint8_t* data, uint32_t size)
{
	FUNCTION_TRACK
	Clear();
	if (NULL == data) return false;
	if (size<8+sizeof(uint32_t)) return false;
	DBUG("==== BEGIN PACKET UNSERIALIZATION =====");
	uint32_t sSize = switchEndian<uint32_t>(*((uint32_t*)data), false);
	uint16_t crc16 = switchEndian<uint16_t>(*((uint16_t*)&data[6]), false);
	DBUG("Size = %u", sSize);
	DBUG("Crc16 = 0x%x", crc16);
	if (size<sSize)
	{
		DBUG("==== END PACKET UNSERIALIZATION (size:%d<sSize:%d) =====", size, sSize);
		return false;
	}

	if (size>8+sizeof(uint32_t))
	{
		uint16_t crc = CalculateCRC16(&data[8+sizeof(uint32_t)], sSize-8-sizeof(uint32_t));
		if (crc16 != crc)
		{
			DBUG("==== END PACKET UNSERIALIZATION (Crc16:0x%x!=OryginalCrc16:0x%x) =====", crc16, crc);
			return false;
		}
	}
	/* all is correct */
	m_id = switchEndian<uint16_t>(*((uint16_t*)&data[4]), false);
	DBUG("Id = %u", m_id);
	m_seq = switchEndian<uint32_t>(*((uint32_t*)&data[8]), false);
	m_response = (m_seq & 0x10000000) != 0;
	m_waiting = (m_seq & 0x20000000) != 0;
	m_seq &= 0x0FFFFFFF;
	DBUG("Seq = %u", m_seq);
	DBUG("Response = %c", m_response?'Y':'N' );
	DBUG("Waiting = %c", m_waiting?'Y':'N' );
	uint32_t pos = 8+sizeof(uint32_t);
	uint32_t idx = 0;
	while(pos<sSize)
	{
		++idx;
		struct FPacketData it;
		it.type = (enum NetworkPacket::EDataType)data[pos++];
		switch(it.type)
		{
			case DATA_BOOL :   it.size = 1; it.data.mBool = (0 != data[pos]); pos++; DBUG("%d - BOOL : %d", idx, it.data.mBool?1:0);break;
			case DATA_S8   :   it.size = 1; it.data.mU8 = *((uint8_t*)&data[pos]); pos++; DBUG("%d - S8 : %d", idx, it.data.mS8); break;
			case DATA_U8   :   it.size = 1; it.data.mU8 = *((uint8_t*)&data[pos]); pos++; DBUG("%d - U8 : %d", idx, it.data.mU8); break;
			case DATA_S16  :   it.size = 2; it.data.mU16 = switchEndian<uint16_t>(*((uint16_t*)&data[pos]), false); pos+=it.size; DBUG("%d - S16 : %d", idx, it.data.mS16); break;
			case DATA_U16  :   it.size = 2; it.data.mU16 = switchEndian<uint16_t>(*((uint16_t*)&data[pos]), false); pos+=it.size; DBUG("%d - U16 : %u", idx, it.data.mU16); break;
			case DATA_S32  :   it.size = 4; it.data.mU32 = switchEndian<uint32_t>(*((uint32_t*)&data[pos]), false); pos+=it.size; DBUG("%d - S32 : %ld", idx, it.data.mS32); break;
			case DATA_U32  :   it.size = 4; it.data.mU32 = switchEndian<uint32_t>(*((uint32_t*)&data[pos]), false); pos+=it.size; DBUG("%d - S32 : %lu", idx, it.data.mU32); break;
			case DATA_S64  :   it.size = 8; it.data.mU64 = switchEndian<uint64_t>(*((uint64_t*)&data[pos]), false); pos+=it.size; DBUG("%d - S64 : %lld", idx, it.data.mS64); break;
			case DATA_U64  :   it.size = 8; it.data.mU64 = switchEndian<uint64_t>(*((uint64_t*)&data[pos]), false); pos+=it.size; DBUG("%d - U64 : %llu", idx, it.data.mU64); break;
			case DATA_FLOAT :  it.size = 4; it.data.mFloat = *((float*)&data[pos]); pos += 4; DBUG("%d - Float : %f", idx, it.data.mFloat);break;
			case DATA_DOUBLE : it.size = 8; it.data.mDouble = *((double*)&data[pos]); pos += 8; DBUG("%d - Double : %lf", idx, it.data.mDouble);break;
			case DATA_STRING : {
			                     uint32_t len = 0;
								 while(data[pos+len]!=0)
								 {
									len++;
								 }
								 len++;
								 it.size = len;
								 it.data.mString = new char[len];
								 memcpy(it.data.mString, &data[pos], len);
								 pos += it.size;
								 DBUG("%d - String : 0x%s : '%s'", idx, ToHex(it.data.mData, it.size).c_str(), it.data.mData);
							   } break;
			case DATA_WIDE_STRING : {
			                     uint32_t len = 0;
								 while(((uint16_t*)(&data[pos]))[len]!=0)
								 {
									len++;
								 }
								 len++;
								 it.size = len*sizeof(uint16_t);
								 it.data.mWideString = new uint16_t[len];
								 memcpy(it.data.mWideString, &data[pos], it.size);
								 pos += it.size;
								 DBUG("%d - WideString : 0x%s", idx, ToHex(it.data.mData, it.size).c_str());
							   } break;
			case DATA_RAW : {
			                     it.size = switchEndian<uint32_t>(*((uint32_t*)&data[pos]), false); pos+=4;
								 it.data.mData = new uint8_t[it.size];
								 memcpy(it.data.mData, &data[pos], it.size);
								 pos += it.size;
								 DBUG("%d - Raw : 0x%s", idx, ToHex(it.data.mData, it.size).c_str());
							   } break;
		};
		m_items.push_back(it);
	}
	DBUG("==== END PACKET UNSERIALIZATION =====");
	return true;
}

NetworkPacket::NetworkPacket(enum COMMUNICATION_ID id)
{
	FUNCTION_TRACK
	m_id = (uint16_t)id;
	m_response = false;
	m_waiting = false;
	m_seq = 0;
}

NetworkPacket::NetworkPacket(const NetworkPacket &request, enum COMMUNICATION_ID responseId)
{
	FUNCTION_TRACK
	m_id = (uint16_t)responseId;
	m_response = false;
	m_waiting = false;
	m_seq = 0;
	if (request.m_waiting)
	{
		m_response = true;
		m_seq = request.m_seq;
	}
}

NetworkPacket::~NetworkPacket()
{
	Clear();
}

void NetworkPacket::CopyFrom(const NetworkPacket &request)
{
	FUNCTION_TRACK
	m_id = request.m_id;
	m_response = request.m_response;
	m_waiting = request.m_waiting;
	m_seq = request.m_seq;
	m_items.clear();
	for(uint32_t i = 0; i< request.m_items.size(); ++i)
	{
		m_items.push_back(request.m_items[i]);
		switch(m_items[i].type)
		{
			case DATA_STRING:
			case DATA_RAW:
			case DATA_WIDE_STRING:
				m_items[i].data.mData = new uint8_t[m_items[i].size];
				memcpy(m_items[i].data.mData, request.m_items[i].data.mData, m_items[i].size);
				break;
		}
	}
}

uint32_t NetworkPacket::GetCRC32() const
{
	boost::crc_32_type result;
	result.process_bytes(&m_id, sizeof(m_id));
	for(uint32_t i = 0; i< m_items.size(); ++i)
	{
		result.process_bytes(&m_items[i].type, sizeof(m_items[i].type));
		switch(m_items[i].type)
		{
			case DATA_BOOL:
            case DATA_S8:
            case DATA_U8:
            case DATA_S16:
            case DATA_U16:
            case DATA_S32:
            case DATA_U32:
            case DATA_S64:
            case DATA_U64:
            case DATA_FLOAT:
            case DATA_DOUBLE:
				result.process_bytes(&m_items[i].data, m_items[i].size);
				break;
			case DATA_STRING:
			case DATA_RAW:
			case DATA_WIDE_STRING:
				result.process_bytes(m_items[i].data.mData, m_items[i].size);
				break;
		}
	}
	return result.checksum();
}

void NetworkPacket::Clear()
{
	FUNCTION_TRACK
	m_id = 0;
	m_seq = 0;
	m_response = false;
	m_waiting = false;
	for(uint32_t i = 0; i<m_items.size(); ++i)
	{
		switch(m_items[i].type)
		{
			case DATA_WIDE_STRING : delete [](m_items[i].data.mWideString);break;
			case DATA_RAW : delete [](m_items[i].data.mData);break;
			case DATA_STRING : delete [](m_items[i].data.mString);break;
		};
	}
	m_items.clear();
}